#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define HAN_EXPORT __attribute((visibility("default")))

HAN_EXPORT int GetCmdResult(const char *cmd, char *ret, unsigned int ret_size)
{
    if (cmd == NULL || ret_size == 0 || ret == NULL)
        return -1;

    FILE *fd = popen(cmd, "r");
    if (fd == NULL) {
        return -1;
    }

    // reads in at most one less than size characters from stream
    // and stores them into the buffer pointed to by s.
    // Reading stops after an EOF or a newline.
    fgets(ret, ret_size, fd);
    pclose(fd);

    int rlen = 0;
    rlen = strlen(ret);

    // get nothing from shell
    if (rlen == 0)
        return -1;

    if (rlen > 0 && ret[rlen - 1] == '\n') {
        ret[rlen - 1] = '\0';
    }

    return 0;
}

int main(void)
{
	char date[64]={0};
	GetCmdResult("date", date, sizeof(date));

	printf("hello world! date = %s \n", date);
	return 0;
}
